import urllib.request as req
import urllib.parse


def create_request(page):
    url='http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=cname'
    data = {
        "cname": "南昌",
        "pid": "",
        "pageIndex": page,
        "pageSize": "10"
    }
    data = urllib.parse.urlencode(data).encode("utf-8")
    header = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36 Edg/107.0.1418.26"
    }
    request=req.Request(url=url,data=data,headers=header)
    return request
def get_content(request):
    res=req.urlopen(request)
    con=res.read().decode("utf-8")
    return con

def down_load(page,con):
    with open(file="./kfcLoc/kfc_locationPage"+str(page)+".json",mode="w",encoding='utf-8') as fp:
        fp.write(con)

if __name__ == '__main__':
    start_page = int(input("请输入起始页码"))
    end_page = int(input("请输入结束页码"))
    for page in range(start_page, end_page+1):
        print(page)
        request=create_request(page) 
        con=get_content(request)
        down_load(page,con)

